{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "",
   "id": "54699cfbbb631954"
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-03-01T05:19:20.004723Z",
     "start_time": "2025-03-01T05:19:19.970514Z"
    }
   },
   "source": [
    "class Node:\n",
    "    def __init__(self,name):\n",
    "        self.name=name\n",
    "        self.children=[]\n",
    "    \n",
    "    def addChild(self,name):\n",
    "        self.children.append(Node(name))\n",
    "        return self\n",
    "    \n",
    "    def deepFirstSearch(self,array=[]):\n",
    "        # global N\n",
    "        array.append(self.name)\n",
    "        for child in self.children:\n",
    "            child.deepFirstSearch(array)\n",
    "            # if child.children==[]:\n",
    "            #    N+=1\n",
    "        return array\n",
    "    \n",
    "root=Node('A')\n",
    "root.addChild('B')\n",
    "root.addChild('C')\n",
    "root.addChild('D')\n",
    "root.children[0].addChild('E')\n",
    "root.children[0].addChild('F')\n",
    "root.children[2].addChild('G')\n",
    "root.children[2].addChild('H')\n",
    "root.children[0].children[1].addChild('I')\n",
    "root.children[0].children[1].addChild('J')\n",
    "root.children[2].children[0].addChild('K')\n",
    "\n",
    "#global N\n",
    "#N=0\n",
    "print(root.deepFirstSearch())\n",
    "#print(N)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['A', 'B', 'E', 'F', 'I', 'J', 'C', 'D', 'G', 'K', 'H']\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-01T04:57:03.397086Z",
     "start_time": "2025-03-01T04:57:03.374568Z"
    }
   },
   "cell_type": "code",
   "source": [
    "class Node:\n",
    "    def __init__(self, name):\n",
    "        self.name = name\n",
    "        self.children = []\n",
    "\n",
    "    def addChild(self, name):\n",
    "        self.children.append(Node(name))\n",
    "        return self\n",
    "\n",
    "    def deepFirstSearch(self, array=None, leaves=0):\n",
    "        if array is None:\n",
    "            array = []\n",
    "        array.append(self.name)\n",
    "        if not self.children:\n",
    "            leaves += 1\n",
    "        for child in self.children:\n",
    "            array, leaves = child.deepFirstSearch(array, leaves)\n",
    "        return array, leaves\n",
    "\n",
    "root = Node('A')\n",
    "root.addChild('B').addChild('C').addChild('D')\n",
    "root.children[0].addChild('E').addChild('F')\n",
    "root.children[2].addChild('G').addChild('H')\n",
    "root.children[0].children[1].addChild('I').addChild('J')\n",
    "root.children[2] .children[0].addChild('K')\n",
    "\n",
    "result, leaves = root.deepFirstSearch()\n",
    "print(result)\n",
    "print(leaves)"
   ],
   "id": "2f62c59b8c74ecc2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['A', 'B', 'E', 'F', 'I', 'J', 'C', 'D', 'G', 'K', 'H']\n",
      "6\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-01T05:30:16.305972Z",
     "start_time": "2025-03-01T05:30:16.282864Z"
    }
   },
   "cell_type": "code",
   "source": [
    "##广度优先搜索bfs\n",
    "class Node:\n",
    "    def __init__(self,name):\n",
    "        self.name=name\n",
    "        self.children=[]\n",
    "    \n",
    "    def addChild(self,name):\n",
    "        self.children.append(Node(name))\n",
    "        return self\n",
    "    \n",
    "    def breadthFirstSearch(self,array=[]):\n",
    "        queue=[self]\n",
    "        while len(queue)>0:\n",
    "            current=queue.pop(0)\n",
    "            array.append(current.name)\n",
    "            queue.extend(current.children)\n",
    "        return array\n",
    "    \n",
    "root=Node('A')\n",
    "root.addChild('B')\n",
    "root.addChild('C')\n",
    "root.addChild('D')\n",
    "root.children[0].addChild('E')\n",
    "root.children[0].addChild('F')\n",
    "root.children[2].addChild('G')\n",
    "root.children[2].addChild('H')\n",
    "root.children[0].children[1].addChild('I')\n",
    "root.children[0].children[1].addChild('J')\n",
    "root.children[2].children[0].addChild('K')\n",
    "\n",
    "print(root.breadthFirstSearch())"
   ],
   "id": "b1a42ec44ef0d5dc",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K']\n"
     ]
    }
   ],
   "execution_count": 8
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
